package com.estate.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.estate.pojo.Floor;
import com.estate.pojo.Housetype;
import com.estate.pojo.Project;
import com.estate.pojo.Room;
import com.estate.pojo.Unit;
import com.estate.service.floor.FloorService;
import com.estate.service.housetype.HouseTypeService;
import com.estate.service.project.ProjectService;
import com.estate.service.room.RoomService;
import com.estate.service.unit.UnitService;
import com.estate.utils.Array;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;

@Controller
public class RoomController {
	@Autowired // 房间服务层
	private RoomService roomService;

	@Autowired // 主项目
	private ProjectService projectService;

	@Autowired // 房间户型
	private HouseTypeService houseTypeService;

	@Autowired // 楼层服务层
	private FloorService floorService;

	@Autowired
	private UnitService unitService;

	// 生成房间跳转路径
	@RequestMapping("/madeFloorAndUnitPath")
	public ModelAndView madeFloorAndUnitPath(ModelAndView model, Integer banId, String banName) {
		List<Housetype> houseTypeList = new ArrayList<>();
		try {
			houseTypeList = houseTypeService.allHouseType();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("Thymeleaf/madeFloorAndUnit");
		model.addObject("banName", banName);
		model.addObject("banId", banId);
		model.addObject("houseTypeList", houseTypeList);
		return model;
	}

	
	
	
	@ResponseBody
	@RequestMapping(value = "/madeFloorAndUnit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public Object madeFloorAndUnit(@RequestBody String roomlist) {
		Floor floor = new Floor();
		Unit unit = new Unit();
		Room room = new Room();
		Map<String, Object> map = new HashMap<String, Object>();
		
		Integer floorNum = null;
		Integer unitNum = null;
		Integer banId = null;
		
		List<Room> roomlists = JSONArray.parseArray(roomlist, Room.class);
		for (Room room1 : roomlists) {
			floorNum = (int)room1.getFloorNum();
			unitNum = (int)room1.getUnitNum();
			banId = (int)room1.getBanId();
		}
		try {
			// 循环添加楼层
			for (int i = 1; i <= floorNum; i++) {
				floor.setFloFloorname(i + "楼");
				floorService.addFloor(floor);
				// 循环添加单元
				for (int j = 1; j <= unitNum; j++) {
					unit.setUnitBanid(banId);
					unit.setUnitUnitname("单元" + j);
					unit.setUnitFloorid(floor.getFloId());
					unitService.addUnit(unit);
					// 循环添加房间
					for (Room rooms : roomlists) {
						room.setRoomNumber(i+"0"+j);//房间号
						room.setRoomHousetypeid(rooms.getRoomHousetypeid());//户型id(外键)
						room.setRoomUnitid(unit.getUnitBanid());//单元id(外键)
						
						room.setRoomStructure(rooms.getRoomStructure());//房间结构
						room.setRoomCoveredArea(rooms.getRoomCoveredArea());//建筑面积
						room.setRoomInnerArea(rooms.getRoomInnerArea());//套内面积
						room.setRoomAreastate(rooms.getRoomAreastate());//面积状态
						
						roomService.addRoom(room);
					}
				}
			}
			map.put("result", "succeed");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "error");
		}
		return JSON.toJSONString(map);
	}

	// 下一步添加房间路径
	@RequestMapping("/madeRoomPath")
	public ModelAndView madeRoomPath(ModelAndView model) {
		model.setViewName("Thymeleaf/madeRoom");
		return model;
	}

	@RequestMapping("/sourceGeneration")
	public ModelAndView SourceGeneration(ModelAndView model,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "currentPageNo", defaultValue = "1") Integer currentPageNo,
			@RequestParam(value = "projectId", defaultValue = "0") Integer projectId,
			@RequestParam(value = "list", defaultValue = "") String list) {
		List<Room> rooms = null;
		PageInfo<Room> page = null;
		PageHelper.startPage(currentPageNo, pageSize);
		/*String str = String.format("%,d", 111199834444444l);
		System.out.println(str);*/
		try {
			rooms = roomService.selectRoom(projectId, list);
			page = new PageInfo<Room>(rooms);
			model.addObject("rooms", rooms);
			model.addObject("page", page);
			List<Project> projects = projectService.getAllProject();
			model.addObject("projects", projects);
			model.setViewName("/Thymeleaf/RoomManagement");
			// 数据回填
			model.addObject("list", list);
			model.addObject("projectId", projectId);
		} catch (Exception e) {
			e.printStackTrace();
			model.setViewName("/Thymeleaf/500");
		}
		return model;
	}

	@RequestMapping("/getByRoomId")
	public ModelAndView selectRoomById(ModelAndView model, @RequestParam(value = "roomId") Integer roomId) {
		try {
			Room room = roomService.selectRoomById(roomId);
			model.addObject("room", room);
			List<Housetype> housetypes = houseTypeService.allHouseType();
			model.addObject("housetypes", housetypes);// 户型
			Array array = new Array();// 工具包下的数组类
			model.addObject("array", array);
			model.setViewName("/Thymeleaf/RoomAdministration");
		} catch (Exception e) {
			e.printStackTrace();
			model.setViewName("/Thymeleaf/500");
		}
		return model;
	}

	@RequestMapping("/upByRoomId")
	@ResponseBody
	public Object updateRoomById(ModelAndView model, Room room) {
		// String ex = null;
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (null != room) {
				String[] strs = room.getRoom_name().split("-");
				for (int i = 0, len = strs.length; i < len; i++) {
					// System.out.println(strs[i].toString());
					switch (i) {
					case 0:
						room.setProject_name(strs[i].toString());
						break;
					case 1: {
						room.setDet_abbreviation(strs[i].toString());
						break;
					}
					case 2: {
						room.setBan_name(strs[i].toString());
						break;
					}
					case 3: {
						room.setRoomNumber(strs[i].toString());
					}
					}
				}
				// ex = roomService.updateRoomById(room);
			}
			/*
			 * model.addObject("ex", ex);// “修改成功”或者“修改失败”
			 * model.setViewName("/Thymeleaf/RoomAdministration");
			 */
			map.put("num", roomService.updateRoomById(room));

		} catch (Exception e) {
			e.printStackTrace();
			model.addObject("ex", "修改失败");
			model.setViewName("/Thymeleaf/500");
		}
		// return model;
		return JSONArray.toJSONString(map);
	}

}
